<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/3/4
 * Time: 16:38
 */

namespace app\admin\controller;

use think\Db;

class Notice extends Main
{
    public function index()
    {
        $data = Db::name('notice')->order('odx asc,timestamp desc')->paginate(15);
        $this->assign('list', $data);
        return $this->fetch('index');
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if (post()) {
            $data = post();
            $insertData['title'] = $data['title'];
            $insertData['content'] = $data['content'];
            $insertData['odx'] = $data['odx'];
            $insertData['type'] = $data['type'];
            $insertData['user_id'] = isession('user_id');
            $insertData['truename'] = ssession('truename');
            $insertData['timestamp'] = time();
            DB::name('notice')->insert($insertData);
            $this->success("添加成功");
        } else {
            return $this->fetch('add');
        }
    }

    /**
     * 更新
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit($id)
    {
        if (post()) {
            $data = post();
            $data['timestamp'] = time();
            DB::name('notice')->where('id', post('id'))->update($data);
            $this->success("更新成功");
        } else {
            $info = DB::name('notice')->where('id', $id)->find();
            $this->assign("info", $info);
            return $this->fetch('edit');
        }
    }

    /**
     * 更新状态
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateType()
    {
        $type = post('type');
        $updateData['type'] = $type;
        $where['id'] = post('id');
        Db::name('notice')->where($where)->update($updateData);
        $this->success('更新成功');
    }

    /**
     * 删除
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delect()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where['id'] = ['in', join(",", $data['id'])];
        } else {
            $where['id'] = $data['id'];
        }
        Db::name('notice')->where($where)->delete();
        $this->success("删除成功");
    }

    /**
     * 详情
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($id)
    {
        $info = DB::name('notice')->where('id', $id)->find();
        $this->assign("info", $info);
        return $this->fetch('info');
    }

    /**
     * 图片上传
     */
    public function upload()
    {
        $dir = '/files/notice/';
        $res = $this->imgUploads($_FILES['file'], $dir, ['jpg', 'jpeg', 'png']);
        if ($res['res'] == true) {
            $arr = array(
                'code' => 0,
                'msg' => '',
                'data' => array(
                    'src' => $res['files']['path'],
                    'title' => $res['files']['name']
                )
            );
        } else {
            $arr = array(
                'code' => 1,
                'msg' => $res['msg']
            );
        }
        echo json_encode($arr);
        exit;
    }
}